/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-4-22
*
\*********************************************************************/
//#include <stdio.h>
//#include <stdlib.h>

#include <thread>
#include <time.h>
#include <random>
#include <sstream>

#include "DataStoreManage.h"

//outfile2<<pod_pitchs[i]<<","<<pod_yaws[i]<<","<<ac_pitchs[i]<<","<<ac_yaws[i]<<","<<ac_rolls[i]<<endl;
//outfile3<<f_times[i]<<endl;
//outfile4<<ac_speed_ds[i]<<","<<ac_speed_bs[i]<<","<<ac_speed_ts[i]<<","<<ac_height_hs[i]<<","<<ac_height_ws[i]<<endl;
//outfile5<<result_filter0s[i]<<","<<result_filter1s[i]<<","<<result_filter2s[i]<<","<<result_filter3s[i]<<","<<result_filter4s[i]<<","<<result_filter5s[i]<<","<<result_filter6s[i]<<","<<result_filter7s[i]<<","<<result_filter8s[i]<<","<<frame_OK<<endl;
//outfile6<<result_bfilter0s[i]<<","<<result_bfilter1s[i]<<","<<result_bfilter2s[i]<<","<<result_bfilter3s[i]<<","<<result_bfilter4s[i]<<","<<result_bfilter5s[i]<<","<<result_bfilter6s[i]<<","<<result_bfilter7s[i]<<","<<result_bfilter8s[i]<<","<<frame_OK<<endl;
//outfile7<<cur.mPointInFrameOut[i].x<<","<<cur.mPointInFrameOut[i].y<<",";


DataStoreManage DataStoreManage::m_Instance;

DataStoreManage& DataStoreManage::GetInstance()
{
    return m_Instance;
}

DataStoreManage::DataStoreManage()
{
//    std::string rootPrefix = "/tmp/databk/";
//    std::string rootPrefix = "/home/root/databk/";
    std::string rootPrefix = "/home/root/data/";

    std::random_device rd;
    std::ostringstream ranName;
    ranName << rd();

//    char ranName[20];
//    clock_t time=clock();
//    sprintf(ranName, "%ld", time);

    results_angle.SetPar(rootPrefix,"results_angle",ranName.str(),"frmae_time(ms),pod_pitchs,pod_yaws,ac_pitchs,ac_yaws,ac_rolls\n");
    results_time.SetPar(rootPrefix,"results_time",ranName.str(),"frmae_time(ms)\n");
    results_ac.SetPar(rootPrefix,"results_ac",ranName.str(),"frmae_time(ms),ac_speed_ds,ac_speed_bs,ac_speed_ts,ac_height_hs,ac_height_ws\n");
    results_filter.SetPar(rootPrefix,"results_filter",ranName.str(),"frmae_time(ms),transverse_filter,longitudinal_filter,vertical_filter,transverse_speed_filter,longitudinal_speed_filter,vertical_speed_filter,ship_pitch_filter,ship_yaw_filter,ship_roll_filter,nav_state_filter\n");
    results_bfilter.SetPar(rootPrefix,"results_bfilter",ranName.str(),"frmae_time(ms),transverse_bfilter,longitudinal_bfilter,vertical_bfilter,transverse_speed_bfilter,longitudinal_speed_bfilter,vertical_speed_bfilter,ship_pitch_bfilter,ship_yaw_bfilter,ship_roll_bfilter,nav_state_bfilter\n");
    results_process.SetPar(rootPrefix,"results_process",ranName.str(),"frmae_time(ms),process_hearer_start,,,,,,,,,,,,,,,,,,,,,,,,,,,,process_hearer_end\n");
    debug_time.SetPar(rootPrefix,"debug_time",ranName.str(),"frmae_time(ms),frame_time1(ms),frame_time2(ms),frame_time3(ms)\n");
}

void DataStoreManage::Store()
{
    results_angle.StoreFlash();
    results_time.StoreFlash();
    results_ac.StoreFlash();
    results_filter.StoreFlash();
    results_bfilter.StoreFlash();
    results_process.StoreFlash();
    debug_time.StoreFlash();
}

void DataStoreManage::StoreThread()
{
    std::thread([&]()
    {
        while(true)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(10000));
            Store();
        }
    }).detach();
}

DataStoreManage::~DataStoreManage()
{
//    DEBUG("DataStoreManage Destructors\n");
}


/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
